package com.hfdg.process.service.impl;

import com.hfdg.process.model.ProcessModel;
import com.hfdg.process.service.TaskHandlerService;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.TaskService;
import org.flowable.engine.impl.persistence.entity.CommentEntity;
import org.flowable.engine.impl.persistence.entity.CommentEntityImpl;
import org.flowable.task.api.Task;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName TaskHandlerServiceImpl
 * @Description TODO
 * @Author yuelh
 * @Date 2020-2-4 0:08
 * @Version 1.0
 * @Company 会飞的锅工作室
 **/
public class TaskHandlerServiceImpl implements TaskHandlerService {
    @Autowired
    RuntimeService runtimeService;
    @Autowired
    RepositoryService repositoryService;
    @Autowired
    TaskService taskService;


    @Override
    public String submit(ProcessModel processModel) {
        CommentEntity commentEntity = new CommentEntityImpl();
        commentEntity.setMessage(processModel.getOpinion());
        commentEntity.setAction(processModel.getOperation());
        commentEntity.setTaskId(processModel.getTaskId());
        commentEntity.setFullMessage(processModel.getOpinion());
        commentEntity.setUserId(processModel.getSubmitterId());
        Map<String,Object> taskParams = new HashMap<>();
        taskParams.put("nextHandlerId",processModel.getNextHandlerId());
        Task task = taskService.createTaskQuery().taskId(processModel.getTaskId()).singleResult();
        commentEntity.setProcessInstanceId(task.getProcessInstanceId());
        taskService.saveComment(commentEntity);
        taskService.complete(processModel.getTaskId(),taskParams);
        return null;
    }
}
